
// 主题切换优化
(function() {
    const html = document.documentElement;
    const btn = document.getElementById('themeToggleBtn');
    const icon = btn.querySelector('i');
    const THEME_KEY = 'chs_theme';

    function setTheme(theme) {
        html.setAttribute('data-theme', theme);
        localStorage.setItem(THEME_KEY, theme);
        icon.className = theme === 'dark' ? 'fas fa-sun me-1' : 'fas fa-moon me-1';
        btn.textContent = theme === 'dark' ? '浅色模式' : '深色模式';
        btn.title = theme === 'dark' ? '切换为浅色模式' : '切换为深色模式';
    }

    function toggleTheme() {
        setTheme(html.getAttribute('data-theme') === 'dark' ? 'light' : 'dark');
    }

    btn.addEventListener('click', toggleTheme);

    // 初始化主题
    const saved = localStorage.getItem(THEME_KEY);
    if (saved === 'dark' || (saved !== 'light' && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
        setTheme('dark');
    } else {
        setTheme('light');
    }
})();